package com.siblings.loan.ui.user.impl

import android.content.Context
import com.lib.base.utils.log.LogHelper
import com.siblings.http.callback.StringCallback
import com.siblings.http.model.Response
import com.siblings.loan.http.IHttpCallback
import com.siblings.loan.ui.user.view.ISibUserFeedbackView
import com.siblings.mvp.presenter.MvpPresenter
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/18 16:26
 * @Annotation：意见反馈 Presenter 实现
 */
class SibUserFeedbackPresenterImpl(context: Context?) : MvpPresenter<ISibUserFeedbackView>(context) {

    private var feedbackModel: SibUserFeedbackModelImpl? = null
    //反馈类型列表
    private var typeList: ArrayList<JSONObject>? = null

    init {
        feedbackModel = SibUserFeedbackModelImpl()
    }

    /**
     * 获取意见反馈类型
     */
    fun loadQuestionTypeListAction() {
        if (typeList == null){
            typeList = arrayListOf()
        }
        typeList!!.clear()
        val dataList = arrayListOf(
            "Product Suggestion,SUGGEST",
            "App bugs,BUG",
            "Payment issues,PAYMENT",
            "Others,OTHER")
        for (i in 0..<dataList.size){
            val items = dataList[i].split(",")
            val obj = JSONObject()
            obj.put("title", items[0])
            obj.put("value", items[1])
            obj.put("isSelect",false)
            typeList!!.add(obj)
        }
        mvpView.setSibQuestionTypeList(typeList!!)
    }

    /**
     * 图片上传
     */
    fun uploadSibImageAction(imgPath: String){
        mvpView.showSibLoading()
        feedbackModel!!.uploadSibImageAction(imgPath,object : StringCallback() {
            override fun onSuccess(response: Response<String>?) {
                when(response!!.code()){
                    200 -> {
                        val data = JSONObject(response.body())
                        when(data.optInt("code")){
                            0 -> {//成功
                                LogHelper.e("image upload success ：$data")
                                val imgUrl = data.optJSONObject("obj")!!.optString("url")
                                mvpView!!.showSibUploadImageSuccess(imgUrl)
                            }
                            else -> {
                                mvpView.showSibToast(data.optString("msg"))
                            }
                        }
                    }
                    else -> {
                        mvpView.showSibToast(response.message())
                    }
                }
            }
            override fun onError(response: Response<String>?) {
                super.onError(response)
                mvpView.showSibToast(response!!.message())
            }

            override fun onFinish() {
                mvpView.hideSibLoading()
            }
        })
    }

    /**
     * 提交反馈
     */
    fun submitSibFeedbackAction(params: JSONObject){
        feedbackModel!!.submitSibFeedbackAction(params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val msg = data.optString("msg")
                mvpView!!.showSibToast(msg)
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView.showSibSubmitSuccess()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }

    /**
     * 获取全部问题类型列表
     */
    fun getTypeList(): ArrayList<JSONObject>{
        return typeList!!
    }
}